package tkatva.consulting.jmxpoller

import tkatva.consulting.jmxconnection.beans._
import scala.collection.mutable.ListBuffer
import java.lang.ClassLoader
import tkatva.consulting.jmxpoller.smxService.impl._



/**
 * @author ${user.name}
 */
object App {
  
  def foo(x : Array[String]) = x.foldLeft("")((a,b) => a + b)
  
  def main(args : Array[String]) {
    
    var  routePoller = new RoutePollerServiceImpl
    var routesInfo = routePoller.getRoutesInfo
    for (routeBean <- routesInfo) {
      for (key <- routeBean.routeInfoAttributes.keys) {
        val vl = routeBean.routeInfoAttributes.get(key)
        val vlStr = vl match {
          case Some(s) => s
          case None => ""
        }
        println("Attr :  " + key +  " value : " + vlStr)
      }
    }
  }

  def jmxTst() = {
    var filePath = ClassLoader.getSystemResource("cfg.xml").getFile()
    var cfg = new CfgHelper
    var res = cfg.getServicemixUrl(filePath)
    var usrNamePword = cfg.getUsrNameAndPword(filePath)
    var filters = cfg.getFilters(filePath)
    
    
    var jmxser = new JmxPoller
    jmxser.srvUrl = res
    jmxser.serviceUsr = usrNamePword._1
    jmxser.servicePword = usrNamePword._2
    jmxser.createConnection
    //var domains = jmxser.getDomains
    
    println("Getting beans")
    var jbeans = jmxser.getSpecifiedBeans(filters)
    for (jbean <- jbeans) {
      println("Type : " + jbean.BeanType)
      if (jbean.SubBeans.size > 0 ) {
        
        for (subBean <- jbean.SubBeans) {
          println("Bean name : " + subBean.BeanName)
          var refreshBean = jmxser.getBeanWithName(subBean.BeanName)
          println("REFRESHED BEAN attr count :  " + refreshBean.BeanOperations.size + " name : " + refreshBean.BeanName)
        }
        
      }
      /*
      println("Found bean : " + jbean.BeanName + " of type " + jbean.BeanType)
      for (key <- jbean.BeanProperties.keys) {
        val value = jbean.BeanProperties.get(key)
        var convertedVal =
        value match {
           case Some(s) => s
           case None  => ""
        }
      println("Property name : " + key + " value : " + convertedVal )
      }
      
      jbean.BeanOperations.foreach(x => println("Operation : " + x.operationName))*/
    }
  }
}
